/-----------------------------------------------/;
/**
 * 懒汉式
 */
class LazySingleton {
  constructor() {
    // 如果实例已经存在，则返回已有实例
    if (LazySingleton.instance) {
      return LazySingleton.instance; // 如果 LazySingleton.instance 已经存在，返回它
    }

    // 初始化实例属性
    this.value = Math.random(); // 示例属性

    // 将当前实例赋值给 LazySingleton.instance
    LazySingleton.instance = this; // 将当前实例保存在 LazySingleton.instance 中
  }

  getValue() {
    return this.value; // 返回实例的 value 属性
  }
}

/**
 * 饿汉式
 */
class HungrySingleton {
  // 在类加载时就创建实例
  static instance = new HungrySingleton();

  constructor() {
    // 如果实例已经存在，则返回已有实例
    if (HungrySingleton.instance) {
      return HungrySingleton.instance; // 如果 HungrySingleton.instance 已经存在，返回它
    }
  }

  getValue() {
    return Math.random(); // 示例属性
  }
}
/-------------------------------------/;

/**
 * 1
 */
class Single11 {
  static getInstance = (function () {
    let singleInstance = null;
    return function () {
      if (!singleInstance) {
        singleInstance = new Single();
      }

      return singleInstance;
    };
  })();
}

class Single12 {
  static instance = new Single2();

  static getInstance() {
    return Single12.instance;
  }
}
const i1 = Single2.getInstance();
const i2 = Single2.getInstance();
console.log(i1 === i2);

/**
 * 2
 */
class Single21 {
  static getInstance = (function () {
    let instance = null;
    return function () {
      if (!instance) {
        instance = new Single1();
      }
      return instance;
    };
  })();
}

class Single22 {
  static instance = new Single22();
  static getInstance = () => {
    return Single22.instance;
  };
}

/**
 * 3
 */
class Single31 {
  static getInstance = (function () {
    let instance = null;
    return function () {
      if (!instance) {
        instance = new Single31();
      }
      return instance;
    };
  })();
}

class Single32 {
  static instance = new Single32();

  static getInstance = () => {
    return Single32.instance;
  };
}

class Single33 {
  constructor() {
    if (Single33.instance) {
      return Single33.instance;
    }

    // 初始化实例属性
    this.value = Math.random(); // 示例属性

    Single33.instance = this;
  }

  getValue() {
    return this.value;
  }
}

class Single34 {
  static instance = new Single34();
  constructor() {
    if (Single34.instance) {
      return Single34.instance;
    }
  }

  getValue() {
    return this.value;
  }
}

class Single41 {
  static getInstance = (function () {
    let instance = null;
    return function () {
      if (!instance) {
        instance = new Single41();
      }
      return instance;
    };
  })();
}

class Single42 {
  static instance = new Single42();

  static getInstance() {
    return Single42.instance;
  }
}

class Single43 {
  constructor() {
    if (Single43.instance) {
      return Single43.instance;
    }

    this.value = Math.random();

    Single43.instance = this;
  }

  getValue() {
    return this.value;
  }
}

class Single44 {
  static instance = new Single44();

  constructor() {
    if (Single44.instance) {
      return Single44.instance;
    }
  }
}

class Single51 {
  static getInstance = (function () {
    let instance = null;
    return function () {
      if (!instance) {
        instance = new Single51();
      }
      return instance;
    };
  })();
}

class Single52 {
  static instance = new Single52();

  static getInstance() {
    return Single52.instance;
  }
}

class Single53 {
  constructor() {
    if (Single53.instance) {
      return Single53.instance;
    }

    Single53.instance = this;
  }
}

class Single54 {
  static instance = new Single54();

  constructor() {
    if (Single53.instance) {
      return Single53.instance;
    }
  }
}
